package com.hzlj.position.locate.controller;


import com.fz.common.base.dto.ResultDTO;
import com.google.common.collect.Maps;
import com.hzlj.position.locate.cache.ReportInspectionCorrectConfigCache;
import com.hzlj.position.locate.common.common.PositionSimpleDTO;
import com.hzlj.position.locate.common.dto.position.PositionCorrectDTO;
import com.hzlj.position.locate.common.dto.reportInspectionCorrect.ReportInspectionCorrectManualDTO;
import com.hzlj.position.locate.common.model.Position;
import com.hzlj.position.locate.service.ReportInspectionCorrectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

/**
 * 【通信联络核查-核查-纠偏】接口
 *
 * @author lifh
 * @date 2023-07-26 14:55:06
 */
@Slf4j
@RestController
@RequestMapping("/position/report/inspection/correct")
public class ReportInspectionCorrectController {

    @Resource
    private ReportInspectionCorrectService reportInspectionCorrectService;
    @Resource
    private ReportInspectionCorrectConfigCache reportInspectionCorrectConfigCache;


    /**
     * 通过位置ID查询纠偏记录
     */
    @GetMapping("/{positionId}")
    public ResultDTO<PositionCorrectDTO> getPositionCorrect(@PathVariable("positionId") String positionId, Date positionTime) {
        log.debug("【查询信息】通信联络核查-核查-纠偏:positionId={},positionTime={}", positionId, positionTime);
        return new ResultDTO<>(reportInspectionCorrectService.getPositionCorrect(positionId, positionTime));
    }

    /**
     * 通信联络核查-核查-纠偏/重新纠偏
     */
    @PostMapping("")
    public ResultDTO<Boolean> correct(@RequestBody ReportInspectionCorrectManualDTO dto) {
        log.info("【新增信息】通信联络核查-核查-纠偏/重新纠偏={}", dto);
        reportInspectionCorrectService.correct(dto);
        return new ResultDTO<>();
    }


    /**
     * 匹配纠偏
     */
    @GetMapping("/match/{jzId}")
    public ResultDTO<Map<String, Object>> getPositionCorrect(@PathVariable("jzId") String jzId,
                                                             String longitude,
                                                             String latitude,
                                                             BigDecimal distance) {
        Map<String, Object> map = Maps.newHashMap();
        PositionSimpleDTO config = reportInspectionCorrectConfigCache.match(jzId, longitude, latitude, distance);
        if (config != null) {
            BigDecimal realDistance = reportInspectionCorrectConfigCache.distance(
                    longitude,
                    latitude,
                    config.getLongitude(),
                    config.getLatitude()
            );
            map.put("distance", realDistance);
            map.put("config", config);
        }
        return new ResultDTO<>(map);
    }

    /**
     * 通过位置ID查询纠偏记录
     */
    @PostMapping("/position")
    public ResultDTO<Boolean> correctPosition(@RequestBody Position position) {
        reportInspectionCorrectService.correct(position);
        return new ResultDTO<>();
    }

}

